﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Actuals.CoreDomain.Business;
using Actuals.Business;
using Actuals.CoreDomain;
using Actuals.Common;




namespace Actuals
{
    public partial class TechnicalTasks : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             if (Session["EmployeeId"] == null)
             {
                 Response.Write("Please login!");
                 Response.End();
             } 
            if (!Page.IsPostBack)
            {
                IBusinessProject businessProject = new BusinessProject();
                IList<IProject> projects = businessProject.GetProjects();
                ddlProject.DataSource = projects;
                ddlProject.DataTextField = "ProjectDescription";
                ddlProject.DataValueField = "ProjectId";
                ddlProject.DataBind();
                ddlProject.Items.Insert(0, "Select");
            }
            else
            {
                if (Request.Form["__EVENTTARGET"] == "refresh")
                {
                    BindGridView();
                }
            }

        }
        
        protected void btnDeleteTechnicalTask_Click(object sender, EventArgs e)
        {

            try
            {


                for (int i = 0; i < gvResults.Rows.Count; i++)
                {
                    CheckBox cb = (CheckBox)gvResults.Rows[i].FindControl("ChkTechnicalTask");

                    Label lbl = (Label)gvResults.Rows[i].FindControl("lblTechnicalTaskId");



                    if (cb.Checked)
                    {


                        string TechnicalTaskId = string.Empty;
                        if (lbl != null)
                        {
                            TechnicalTaskId = lbl.Text;

                            BusinessTechnicalTask businesstechnicalTask = new BusinessTechnicalTask();

                            businesstechnicalTask.DeleteTechnicalTask(int.Parse(TechnicalTaskId));
                        }
                    }
                }

                lblMsg.Text = "Deleted";
                BindGridView();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }
        }









        protected void FillRelease(string projectId)
        {
            IBusinessRelease businessRelease = new BusinessRelease();
            IList<IRelease> releases = businessRelease.GetReleases(int.Parse(projectId));

            ddlRelease.DataSource = releases;
            ddlRelease.DataTextField = "Description";
            ddlRelease.DataValueField = "ReleaseId";
            ddlRelease.DataBind();
            ListItem select = new ListItem();
            select.Value = "0";
            select.Text = "Select";
            ddlRelease.Items.Insert(0, select);
        }
        protected void ddlProject_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            FillRelease(ddlProject.SelectedValue);
        }




        protected void FillTask(string projectId, string releaseId)
        {
            IBusinessTask businessTask = new BusinessTask();
            IList<ITask> tasks = businessTask.GetTasks(int.Parse(projectId), int.Parse(releaseId));

            ddlTask.DataSource = tasks;
            ddlTask.DataTextField = "TaskShortName";

            ddlTask.DataValueField = "TaskId";
            ddlTask.DataBind();
            ddlTask.Items.Insert(0, "Select");
        }

        protected void ddlRelease_OnSelectedIndexChanged(Object sender, EventArgs e)
        {
            FillTask(ddlProject.SelectedValue, ddlRelease.SelectedValue);
        }
        protected void FillUserStory(string taskId)
        {
            IBusinessUserStory businessUserStory = new BusinessUserStory();
            IList<IUserStory> userStory = businessUserStory.GetUserStories(int.Parse(taskId));
            ddlUserStory.DataSource = userStory;

            ddlUserStory.DataTextField = "UserStoryDescription";
            ddlUserStory.DataValueField = "UserStoryId";
            ddlUserStory.DataBind();
            ddlUserStory.Items.Insert(0, "Select");
        }
        protected void ddlTask_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            FillUserStory(ddlTask.SelectedValue);
        }


        protected void BindGridView()
        {
            int userStoryId = (int.Parse(ddlUserStory.SelectedValue));
            IBusinessTechnicalTask businessTechnicalTask = new BusinessTechnicalTask();
            IList<ITechnicalTask> technicalTasks = businessTechnicalTask.GetTechnicalTasks(userStoryId);
            ViewState["DataSource"] = technicalTasks;
            gvResults.DataSource = technicalTasks;
            gvResults.DataBind();

        }
        protected void btnGetTechnicalTask_Click(Object sender, EventArgs e)
        {
            BindGridView();
        }

        protected void gvResults_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            string technicalTaskId;

            switch (e.CommandName)
            {
                case "EditTechnicalTask":

                    technicalTaskId = e.CommandArgument.ToString();
                    ClientScript.RegisterStartupScript(this.GetType(), "UpdateTechnicalTask", "UpdateTechnicalTask('" + technicalTaskId + "');", true);

                    //Response.Redirect("AddTechnicalTasks.aspx?Mode=Edit");
                    break;


                /*case "DeleteTechnicalTask":
                    technicalTaskId = e.CommandArgument.ToString();
                    BusinessTechnicalTask businesstechnicalTask = new BusinessTechnicalTask();
                    ITechnicalTask technicalTask = new TechnicalTask();
                    technicalTask.TechnicalTaskId = Int32.Parse(technicalTaskId);
                    businesstechnicalTask.DeleteTechnicalTask(technicalTask);
                    break;*/


            }
        } 

        protected string getSprint(object sprintId)
        {
            IBusinessSprint businessSprint = new BusinessSprint();
            ISprint sprint = businessSprint.GetSprint(int.Parse(sprintId.ToString()));
            return sprint.SprintDescription;
        }

        protected string getStatusName(object status)
        {
            IBusinessStatus businessStatus = new BusinessStatus();
            string statusName = businessStatus.getStatusName(char.Parse(status.ToString()));
            return statusName;

        }
        protected string getTaskCompletedBy(object employeeId)
        {
            IBusinessEmployee businessEmployee = new BusinessEmployee();
            IEmployee employee = businessEmployee.GetEmployee(int.Parse(employeeId.ToString()));
            return employee.NickName;
        }
        protected string getCodeRevStatus(object codeRevStatus)
        {
            IBusinessCodeRevStatus businessCodeRevStatus = new BusinessCodeRevStatus();
            string codeRevStatusName = businessCodeRevStatus.getCodeRevStatus(char.Parse(codeRevStatus.ToString()));
            return codeRevStatusName;
        }

        protected string getCodeReviewDoneBy(object employeeId)
        {
            string employeeNickName = string.Empty;

            if (employeeId != null)
            {
                IBusinessEmployee businessEmployee = new BusinessEmployee();
                IEmployee employee = businessEmployee.GetEmployee(int.Parse(employeeId.ToString()));
                employeeNickName = employee.NickName;
            }

            return employeeNickName; 
        }















    }
}



